package cn.com.xy.sms.sdk.ui.popu.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;

public class DuoquProgressBarTable extends RelativeLayout {

    private int mMarginTop = 0;
    private int mMarginLeft = 0;
    private int mMarginRight = 0;
    private int mMarginBottom = 0;
    private int mLayoutId = 1000;

    LayoutInflater mLayoutInflater = null;

    protected int mChildId = 0;
    protected List<DuoquProgressVerHolder> mViewHolderList = null;
    Context context;

    private DuoquProgressViewShowMoreInfo mDuoquProgressViewShowMoreInfo = null;
    private boolean mExpanded = false;

    public DuoquProgressBarTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initParams(context, attrs);
        mDuoquProgressViewShowMoreInfo = new DuoquProgressViewShowMoreInfo(this);
    }

    public TextView getFirstContentView() {
        if (mViewHolderList != null && mViewHolderList.size() > 0) {
            return mViewHolderList.get(0).leftContentView;
        }
        return null;
    }

    public void setContentList(BusinessSmsMessage message, int dataSize, String dataKey, boolean isRebind) {
        try {
            if (dataSize == 0) {
                this.setVisibility(View.GONE);
                return;
            }

            mExpanded = mDuoquProgressViewShowMoreInfo.getExpanded(message);
            mDuoquProgressViewShowMoreInfo.clearMoreInfoViewHolderList();

            this.setVisibility(View.VISIBLE);
            final List<DuoquProgressVerHolder> holderList = mViewHolderList;
            if (isRebind && holderList != null) {
                int holderSize = holderList.size();
                int diffSize = holderSize - dataSize;
                DuoquProgressVerHolder tempHolder = null;
                if (diffSize > 0) {
                    for (int i = 0; i < holderSize; i++) {
                        tempHolder = holderList.get(i);
                        if (i < dataSize) {
                            tempHolder.setVisibility(View.VISIBLE);
                            tempHolder.setContent(i, message, dataKey, isRebind);
                            showOrHiddenMoreInfo(i);
                        } else {
                            tempHolder.setVisibility(View.GONE);
                        }
                    }
                } else {
                    for (int i = 0; i < dataSize; i++) {
                        if (i < holderSize) {
                            tempHolder = holderList.get(i);
                            tempHolder.setVisibility(View.VISIBLE);
                            tempHolder.setContent(i, message, dataKey, isRebind);
                        } else {
                            getHolder(i, message, dataSize, dataKey, false);
                        }
                        showOrHiddenMoreInfo(i);
                    }
                }
                setShowHiddenMoreInfoButtonVisibility(message, dataSize);
                return;
            }
            mViewHolderList = new ArrayList<DuoquProgressVerHolder>();
            mChildId = 0;
            this.removeAllViews();
            for (int i = 0; i < dataSize; i++) {
                getHolder(i, message, dataSize, dataKey, false);
                showOrHiddenMoreInfo(i);
            }
            setShowHiddenMoreInfoButtonVisibility(message, dataSize);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private LayoutParams getRelativeLayoutParams(int viewId) {
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        if (viewId > 1001) {
            int prevLayoutId = viewId - 1;
            params.addRule(RelativeLayout.BELOW, prevLayoutId);
            // params.setMargins(0, mLineSpacing, 0, 0);
        }
        return params;
    }

    protected void initParams(Context context, AttributeSet attrs) {
        TypedArray duoquTbAttr = context.obtainStyledAttributes(attrs, R.styleable.duoqu_table_attr);
        mMarginTop = Math.round(duoquTbAttr.getDimension(R.styleable.duoqu_table_attr_margin_top, 0));
        mMarginLeft = Math.round(duoquTbAttr.getDimension(R.styleable.duoqu_table_attr_margin_left, 0));
        mMarginRight = Math.round(duoquTbAttr.getDimension(R.styleable.duoqu_table_attr_margin_right, 0));
        mMarginBottom = Math.round(duoquTbAttr.getDimension(R.styleable.duoqu_table_attr_margin_bottom, 0));

        duoquTbAttr.recycle();
        if (mMarginTop != 0 || mMarginLeft != 0 || mMarginBottom != 0 || mMarginRight != 0) {
            // the row width and height
            LayoutParams rp = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            rp.setMargins(mMarginLeft, mMarginTop, mMarginRight, mMarginBottom);
            this.setLayoutParams(rp);
        }
    }

    /**
     * create and set TextView
     *
     * @param pos
     * @param message
     * @param dataSize
     * @param dataKey
     */
    @SuppressLint("ResourceAsColor")
    protected void getHolder(int pos, BusinessSmsMessage message, int dataSize, String dataKey, boolean isRebind) {
        DuoquProgressVerHolder holder = new DuoquProgressVerHolder(this.getContext());

        if (mLayoutInflater == null) {
            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        holder.mUsedLayout = (LinearLayout) mLayoutInflater.inflate(R.layout.duoqu_progressbar_table_item, null);
        holder.mUsedLayout.setId(++mLayoutId);
        holder.leftTitleView = (TextView) holder.mUsedLayout.findViewById(R.id.lefttitle);
        holder.leftContentView = (TextView) holder.mUsedLayout.findViewById(R.id.leftcontent);
        holder.progressBar = (ProgressBar) holder.mUsedLayout.findViewById(R.id.progressbar);
        holder.rightHorizedContView = (TextView) holder.mUsedLayout.findViewById(R.id.right_horized_cont);

        this.addView(holder.mUsedLayout, getRelativeLayoutParams(mLayoutId));
        holder.setContent(pos, message, dataKey, isRebind);
        mViewHolderList.add(holder);
    }

    /**
     * 根据展开状态显示或隐藏更多信息
     *
     * @param pos
     */
    private void showOrHiddenMoreInfo(int pos) {
        if (pos < mDuoquProgressViewShowMoreInfo.getDefaultShowRow() || mViewHolderList == null
                || mViewHolderList.size() < pos) {
            return;
        }

        if (pos >= mDuoquProgressViewShowMoreInfo.getmMaxShowInfo()) {
            mViewHolderList.get(pos).setVisibility(View.GONE);
            return;
        }

        DuoquProgressVerHolder viewHolder = (DuoquProgressVerHolder) mViewHolderList.get(pos);
        mDuoquProgressViewShowMoreInfo.addMoreInfoViewHolder(viewHolder);
        // 根据展开状态显示或隐藏更多信息
        viewHolder.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
    }

    /**
     * 根据当前数据数量显示或隐藏展开收起控件
     *
     * @param message
     * @param dataSize
     *            当前数据数量
     */
    private void setShowHiddenMoreInfoButtonVisibility(final BusinessSmsMessage message, int dataSize) {
        if (dataSize <= mDuoquProgressViewShowMoreInfo.getDefaultShowRow()) {
            mDuoquProgressViewShowMoreInfo.hiddenExpandCollapseMoreInfoView();
        } else {
            int lastpos = mDuoquProgressViewShowMoreInfo.getmMaxShowInfo() - 1;
            if (lastpos < 0 || lastpos >= mViewHolderList.size()) {
                lastpos = mViewHolderList.size() - 1;
            }
            int lastItemId = ((DuoquProgressVerHolder) mViewHolderList.get(lastpos)).mUsedLayout.getId();
            mDuoquProgressViewShowMoreInfo.showExpandCollapseMoreInfoView(message, mExpanded, lastItemId);
        }
    }

    /**
     * 设置默认显示数据行数, 最大显示数据行数
     *
     * @param defaultShowRow
     */
    public void setDefaultShowRow(int defaultShowRow, int maxShowRow) {
        mDuoquProgressViewShowMoreInfo.setDefaultShowRow(defaultShowRow);
        mDuoquProgressViewShowMoreInfo.setmMaxShowInfo(maxShowRow);
    }
}
